package com.bst;

import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;


import javax.swing.*;
import java.awt.*;

public class BaiduTranslateConfigurable implements Configurable {
    private JPanel mainPanel;
    private JTextField appIdField;
    private JTextField appKeyField;
    private JComboBox<String> fromLangCombo;
    private JComboBox<String> toLangCombo;

    private final BaiduTranslateSettings settings;

    public BaiduTranslateConfigurable() {
        this.settings = BaiduTranslateSettings.getInstance();
        initUI();
    }

    private void initUI() {
        mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // APP ID
        gbc.gridx = 0;
        gbc.gridy = 0;
        mainPanel.add(new JLabel("百度翻译APP ID:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        appIdField = new JTextField();
        mainPanel.add(appIdField, gbc);

        // 密钥
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0;
        mainPanel.add(new JLabel("百度翻译密钥:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        appKeyField = new JTextField();
        mainPanel.add(appKeyField, gbc);

        // 源语言
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.weightx = 0;
        mainPanel.add(new JLabel("默认源语言:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.weightx = 1.0;
        fromLangCombo = new JComboBox<>(new String[]{"auto", "zh", "en", "ja", "ko", "fr", "de"});
        mainPanel.add(fromLangCombo, gbc);

        // 目标语言
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.weightx = 0;
        mainPanel.add(new JLabel("默认目标语言:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.weightx = 1.0;
        toLangCombo = new JComboBox<>(new String[]{"zh", "en", "ja", "ko", "fr", "de"});
        mainPanel.add(toLangCombo, gbc);

        // 填充空白区域
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 2;
        gbc.weighty = 1.0;
        mainPanel.add(new JPanel(), gbc);
    }

    @Nls(capitalization = Nls.Capitalization.Title)
    @Override
    public String getDisplayName() {
        return "百度翻译配置";
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        loadSettings();
        return mainPanel;
    }

    @Override
    public boolean isModified() {
        return !appIdField.getText().equals(settings.appId) ||
                !appKeyField.getText().equals(settings.appKey) ||
                !fromLangCombo.getSelectedItem().toString().equals(settings.defaultFrom) ||
                !toLangCombo.getSelectedItem().toString().equals(settings.defaultTo);
    }

    @Override
    public void apply() throws ConfigurationException {
        settings.appId = appIdField.getText().trim();
        settings.appKey = appKeyField.getText().trim();
        settings.defaultFrom = fromLangCombo.getSelectedItem().toString();
        settings.defaultTo = toLangCombo.getSelectedItem().toString();
    }

    @Override
    public void reset() {
        loadSettings();
    }

    private void loadSettings() {
        appIdField.setText(settings.appId);
        appKeyField.setText(settings.appKey);
        fromLangCombo.setSelectedItem(settings.defaultFrom);
        toLangCombo.setSelectedItem(settings.defaultTo);
    }
}
